package org.apache.cassandra.gui.component.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class KeyDialog extends JDialog {
  private static final long serialVersionUID = -6904488663855720774L;

  private class EnterAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      enterAction();
    }
  }

  private JTextField keyText = new JTextField();
  private boolean cancel = true;
  private String key;

  public KeyDialog() {
    keyText.addActionListener(new EnterAction());

    JPanel inputPanel = new JPanel(new GridLayout(1, 2));
    inputPanel.add(new JLabel("key: "));
    inputPanel.add(keyText);

    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        enterAction();
      }
    });
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        setVisible(false);
      }
    });

    JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(ok);
    buttonPanel.add(cancel);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(inputPanel, BorderLayout.CENTER);
    panel.add(buttonPanel, BorderLayout.SOUTH);

    add(panel);

    pack();
    setModalityType(ModalityType.DOCUMENT_MODAL);
    setTitle("input key");
    setLocationRelativeTo(null);
    setModal(true);
  }

  private void enterAction() {
    if (keyText.getText().isEmpty()) {
      JOptionPane.showMessageDialog(null, "Enter key.");
      keyText.requestFocus();
      return;
    }

    key = keyText.getText();

    setVisible(false);
    cancel = false;
  }

  /**
   * @return the cancel
   */
  public boolean isCancel() {
    return cancel;
  }

  /**
   * @return the key
   */
  public String getkey() {
    return key;
  }
}
